import type {WebsiteConfigDTO} from "~/interface/WebsiteConfigDTO";

export const ThemeSwitcher = {
    toggleTheme() {
        const root = document.documentElement;
        const currentTheme = root.getAttribute("theme");
        const newTheme = currentTheme === "dark" ? "light" : "dark";
        root.setAttribute("theme", newTheme);
        // 设置 cookie，过期时间为1天
        const expires = `; expires=${new Date(Date.now() + 86400000).toUTCString()}`;
        document.cookie = `auth-theme=${newTheme}${expires}; path=/`;
    },
    initialize() {
        const root = document.documentElement;
        const themeCookie = decodeURIComponent(document.cookie.replace(/(?:(?:^|.*;\s*)auth-theme\s*\=\s*([^;]*).*$)|^.*$/, "$1"));
        const themeToggle = document.getElementById("theme-toggle");
        if (themeToggle) {
            themeToggle.addEventListener("click", this.toggleTheme);
        }
        root.setAttribute("theme", themeCookie || "light");
    }
};
export const useAppStore = defineStore('appStore', {
    state: () => {
        return {
            appLoading: false,
            websiteConfig: {} as WebsiteConfigDTO,
            categoryCount: 0,
            tagCount: 0,
            commentCount: 0
        }
    },
    actions: {
        initializeTheme() {
            ThemeSwitcher.initialize()
        },
    }
})
